/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MNetSockets.CmdChanel.Test;

import MNetSockets.CmdChanel.*;
import java.io.IOException;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author wcss
 */
public class TCPTestFrame extends javax.swing.JFrame implements IDataReader,IConnectionState {

    private CmdServer cs = new CmdServer();
    private CmdClient cc = new CmdClient();
    
    /**
     * Creates new form TCPTestFrame
     */
    public TCPTestFrame() 
    {
        initComponents();
        
        cs.setDataReadEvent(this);
        cc.setDataReadEvent(this);
        cs.setConnectionEvent(this);
        cc.setConnectionEvent(this);
        
        try {
            cs.listen(8801);
        } catch (Exception ex) {
            Logger.getLogger(TCPTestFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        try {
            cc.connect("192.168.0.151", 8801);
        } catch (Exception ex) {
            Logger.getLogger(TCPTestFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtClient = new javax.swing.JTextArea();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtServer = new javax.swing.JTextArea();
        btnSend = new javax.swing.JButton();
        btnRestart = new javax.swing.JButton();
        btnReconnect = new javax.swing.JButton();
        btnCloseServer = new javax.swing.JButton();
        btnCloseClient = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("客户端");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(590, 40, 100, 30));

        jLabel2.setText("服务器");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 40, 100, 30));

        txtClient.setColumns(20);
        txtClient.setRows(5);
        jScrollPane1.setViewportView(txtClient);

        getContentPane().add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(450, 80, 370, 420));

        txtServer.setColumns(20);
        txtServer.setRows(5);
        jScrollPane2.setViewportView(txtServer);

        getContentPane().add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 80, 370, 420));

        btnSend.setText("测试发送");
        btnSend.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSendActionPerformed(evt);
            }
        });
        getContentPane().add(btnSend, new org.netbeans.lib.awtextra.AbsoluteConstraints(720, 10, 100, -1));

        btnRestart.setText("重新监听");
        btnRestart.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnRestartActionPerformed(evt);
            }
        });
        getContentPane().add(btnRestart, new org.netbeans.lib.awtextra.AbsoluteConstraints(240, 10, 90, -1));

        btnReconnect.setText("重新连接");
        btnReconnect.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReconnectActionPerformed(evt);
            }
        });
        getContentPane().add(btnReconnect, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 10, 90, -1));

        btnCloseServer.setText("关闭服务");
        btnCloseServer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseServerActionPerformed(evt);
            }
        });
        getContentPane().add(btnCloseServer, new org.netbeans.lib.awtextra.AbsoluteConstraints(40, 10, -1, -1));

        btnCloseClient.setText("关闭客户");
        btnCloseClient.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCloseClientActionPerformed(evt);
            }
        });
        getContentPane().add(btnCloseClient, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 10, -1, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSendActionPerformed
        // TODO add your handling code here:
        cc.send(new Date().toString() + "对了");
    }//GEN-LAST:event_btnSendActionPerformed

    private void btnRestartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnRestartActionPerformed
        try {
            // TODO add your handling code here:
            //cs.close();
            cs.listen(8801);
        } catch (Exception ex) {
            Logger.getLogger(TCPTestFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnRestartActionPerformed

    private void btnReconnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReconnectActionPerformed
        try {
            // TODO add your handling code here:
            //cc.close();
            cc.connect("192.168.0.151", 8801);
        } catch (Exception ex) {
            Logger.getLogger(TCPTestFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnReconnectActionPerformed

    private void btnCloseServerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseServerActionPerformed
        try {
            // TODO add your handling code here:
            cs.close();
        } catch (Exception ex) {
            Logger.getLogger(TCPTestFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnCloseServerActionPerformed

    private void btnCloseClientActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCloseClientActionPerformed
        try {
            // TODO add your handling code here:
            cc.close();
        } catch (Exception ex) {
            Logger.getLogger(TCPTestFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnCloseClientActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TCPTestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TCPTestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TCPTestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TCPTestFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TCPTestFrame().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCloseClient;
    private javax.swing.JButton btnCloseServer;
    private javax.swing.JButton btnReconnect;
    private javax.swing.JButton btnRestart;
    private javax.swing.JButton btnSend;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextArea txtClient;
    private javax.swing.JTextArea txtServer;
    // End of variables declaration//GEN-END:variables

    @Override
    public void dataRead(Object sender, Object data)
    {
        if (((CmdClient)sender).isSubClient())
        {
           this.txtServer.setText(this.txtServer.getText() + "服务器接收" + ":"  + data + "\n");
           ((CmdClient)sender).send("收到" + new Date().toString() + "！");
        }else
        {
           this.txtClient.setText(this.txtClient.getText() + "客户端接收" + ":"  + data + "\n");
        }
    }

    @Override
    public void changeState(Object sender, String state, String tag) {
        try {
            if (((CmdClient) sender).isSubClient()) {
                this.txtServer.setText(this.txtServer.getText() + "服务器状态" + ":" + state + "\n");
            } else {
                this.txtClient.setText(this.txtClient.getText() + "客户端状态" + ":" + state + "\n");
            }
        } catch (Exception ex) {
            this.txtServer.setText(this.txtServer.getText() + "服务器状态" + ":" + state + "\n");
        }
    }
}
